package com.github.ilioili.justdoit.components.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.model.dao.CategoryDao;
import com.github.ilioili.justdoit.model.pojo.Category;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import java.util.List;

@Layout(R.layout.activity_category)
public class CategoryListActivity extends AppBaseActivity {
    @Id(R.id.toolbar)
    private Toolbar toolbar;
    @Id(R.id.recyclerView)
    private RecyclerView recyclerView;
    private List<Category> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecyclerView.Adapter<Holder>() {
            @Override
            public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = getLayoutInflater().inflate(R.layout.list_category_item, parent, false);
                return new Holder(view);
            }

            @Override
            public void onBindViewHolder(Holder holder, int position) {
                Category category = list.get(position);
                holder.tvCategoryName.setText(category.name);
                holder.ivColorIndicator.setColorFilter(category.color);
                holder.itemView.setTag(category);
            }

            @Override
            public int getItemCount() {
                return null == list ? 0 : list.size();
            }
        });
        loadDataAndFreshUi();
    }

    public static OnCategorySelectListener onCategorySelectListener;

    public static Intent newIntent(Context context, OnCategorySelectListener listener) {
        onCategorySelectListener = listener;
        Intent it = new Intent(context, CategoryListActivity.class);
        return it;
    }

    private void loadDataAndFreshUi() {
        CategoryDao.getAllCategory(new SqlCallback<List<Category>>() {
            @Override
            public void onSucess(List<Category> categories) {
                list = categories;
                recyclerView.getAdapter().notifyDataSetChanged();
            }
        });
    }

    public void onClickNewGroup(View v) {
        startActivityForResult(CreateOrModifyCategoryActivity.newIntent(this), 0);
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (Activity.RESULT_OK == resultCode) {
            loadDataAndFreshUi();
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onCategorySelectListener.onCategorySelect((Category) v.getTag());
            finish();
        }
    };

    class Holder extends RecyclerView.ViewHolder {


        TextView tvCategoryName;
        ImageView ivColorIndicator;

        public Holder(View itemView) {
            super(itemView);
            ivColorIndicator = (ImageView) itemView.findViewById(R.id.iv_category_color);
            tvCategoryName = (TextView) itemView.findViewById(R.id.tv_category_name);
            itemView.setOnClickListener(onClickListener);
        }
    }

    public interface OnCategorySelectListener {
        void onCategorySelect(Category category);
    }

}
